package com.company;

import java.lang.ref.Cleaner;

class Member implements Runnable {
    private final String name;
    public Member(String name) {
        this.name = name;
        System.out.println(name + " is built");
    }
    @Override
    public void run() {
        System.out.println(name + " is destroyed");
    }
}

class JavaAutoClosable implements AutoCloseable {
    private static final Cleaner cleaner = Cleaner.create();

    private final Cleaner.Cleanable cleanable;

    public JavaAutoClosable() {
        Member member = new Member("abc");
        this.cleanable = JavaAutoClosable.cleaner.register(this, member);
    }

    @Override
    public void close() throws Exception {
        this.cleanable.clean();
        System.out.println("close");
    }

    public void doSomething() {
        //
    }
}

public class JavaRuntime {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        System.out.println("start timestamp: " + System.currentTimeMillis());
        Runtime run = Runtime.getRuntime();
        System.out.println(run.availableProcessors());
        System.out.println(AkabaNum.humanizeByteSize(run.maxMemory())); // 默认为1/4
        System.out.println(AkabaNum.humanizeByteSize(run.freeMemory())); // 默认为1/64
        System.out.println(AkabaNum.humanizeByteSize(run.totalMemory()));
        long endTime = System.currentTimeMillis();
        System.out.println("end timestamp: " + (endTime - startTime));
        try (JavaAutoClosable jac = new JavaAutoClosable()) {
            jac.doSomething();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
